import { _decorator, Color, Component, Label, Node, ProgressBar } from 'cc';
import { Global } from '../../global';
const { ccclass, property } = _decorator;

@ccclass('initResources')
export class initResources extends Component {
    // 血量进度条
    @property(ProgressBar)
    hpBar: ProgressBar = null;
    // 血量值
    @property(Label)
    hpLabel: Label = null;
    // 更新时间
    updateTime: number = 0;
    // 已经过去的时间
    passTime: number = 0;

    protected onLoad(): void {
        let params = {
            sign: Global.UsrMg.sign,
            userid: Global.UsrMg.userid
        }
        Global.http.sendRequest('/getResourcesInfo', 'GET', params, (res) => {
            if (res.code == 1) {
                console.log('resourcesInfo err', res.msg);
            }
            this.initResources(res.data);
            this.setResources();
        }, Global.cityServerUrl);
        this.updateTime = Global.ResourcesUpdate.updateTime;
    }

    // 初始化资源
    initResources(data) {
        let hp = {
            count: 0,
            allCount: 0,
            addRate: 0
        };
        for (let i = 0; i < data.length; i++) {
            let count = this.strToNum(data[i].count);
            let allCount = this.strToNum(data[i].allCount);
            let addRate = this.strToNum(data[i].addRate);
            switch (data[i].type) {
                case 'blood':
                    hp.count = count;
                    hp.allCount = allCount;
                    hp.addRate = addRate;
                    Global.Resources.push(hp);
                    break;
                default:
                    break;
            }
        }
    }

    strToNum(str: number) {
        return Math.floor(str * 100) / 100;
    }

    strToNum2(str: number) {
        return Math.floor(str * 1000000) / 1000000;
    }

    // 设置资源
    setResources() {
        let count = Global.Resources[0].count;
        if (count > 10) {
            this.hpLabel.string = this.strToNum(count) + '';
            // 变成绿色
            this.hpLabel.color = new Color(0, 255, 0);
        } else {
            this.hpLabel.string = this.strToNum(count) + '';
            // 变成红色
            this.hpLabel.color = new Color(255, 0, 0);
        }
        let allCount = Global.Resources[0].allCount;
        this.hpBar.progress = count / allCount;
    }

    // 自动更新资源
    updateResources(time:number) {
        const tempTime = time / 3600;
        const addRate = Global.Resources[0].addRate;
        const count = this.strToNum2(Global.Resources[0].count + tempTime * addRate); // 保留小数点后6位 + 现在的数量
        const allCount = Global.Resources[0].allCount;
        if (count >= allCount) {
            Global.Resources[0].count = allCount;
        } else {
            Global.Resources[0].count = count;
        }
    }

    protected update(dt: number): void {
        this.passTime += dt;
        if (this.passTime >= this.updateTime) {
            this.updateResources(this.passTime);
            this.setResources();
            this.passTime = 0;
        }
    }
}


